﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace SAHCPIMS_final
{
    public partial class CREATEACCOUNT : Form
    {
        public int doFlag = 0;
        LOGIN login = new LOGIN();

        public CREATEACCOUNT()
        {
            InitializeComponent();
        }

        private void registerButton_Click(object sender, EventArgs e)
        {
            if (vcode.Text != "sahcpims2010")
            {
                MessageBox.Show("Sorry, you can not create an account.\nInvalid validation code.");
            }
            else
            {
                //this string will be the query string for the connection to the database
                string accountString =
                    @"Provider=Microsoft.Jet.OLEDB.4.0;"
                    + @"Data source=..\\accounts.mdb";


                OleDbConnection account = new OleDbConnection(accountString);
                DataSet detaSet = new DataSet();

                /** ============= Updated by: Monica Tosino :: Form Checking :: March 2, 2010 ============= **/
                if (usernametextBox.Text == "" || passtextBox.Text == ""
                   || verifyPassTextBox.Text == "" || employeeNumberTextBox.Text == ""
                   || fnameTextBox.Text == "" || mnameTextBox.Text == "" || lnameTextBox.Text == "")
                {
                    MessageBox.Show("Input field(s) empty");
                }

                else if (employeeNumberTextBox.Text.Length != 9)
                {
                    MessageBox.Show("Invalid Employee Number.");
                }
                /** =============================== End of Update =============================== **/

                else if (!passtextBox.Text.ToString().Equals(verifyPassTextBox.Text.ToString()))
                {
                    MessageBox.Show("Input Password and Verification not the same. Try Again.");
                    passtextBox.ResetText();
                    verifyPassTextBox.ResetText();
                }

                else
                {
                    //opens account database
                    account.Open();

                    //for connection to the database
                    try
                    {
                        OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into accounts "
                                                  + "values('" + usernametextBox.Text.ToString() + "','" + passtextBox.Text.ToString() + "','" + int.Parse(employeeNumberTextBox.Text) + "','" + fnameTextBox.Text + "','" + mnameTextBox.Text + "','" + lnameTextBox.Text + "')", account);

                        tagalagay.Fill(detaSet);
                        doFlag = 0;

                    }
                    //catches exception if the user input is invalid
                    catch (FormatException)
                    {

                        MessageBox.Show("Invalid Input. Try Again.");
                        doFlag = 1;

                    }
                    //catches exception if the input is already in the database or the input is not saved in the database
                    catch (OleDbException)
                    {
                        MessageBox.Show("Data not Saved in Database. Data Already Exists.");
                        doFlag = 1;

                    }

                    //closes accounts database
                    account.Close();

                    //if registration is successful, the form is disabled
                    if (doFlag == 0)
                    {
                        this.uname.Text = this.usernametextBox.Text;
                        this.name.Text = this.fnameTextBox.Text + " " + this.mnameTextBox.Text + " " + this.lnameTextBox.Text;
                        this.empno.Text = this.employeeNumberTextBox.Text;

                        this.signup.Visible = false;
                        this.success.Visible = true;
                    }
                }
            }
        }

        private void back_Click(object sender, EventArgs e)
        {
            LOGIN back = new LOGIN();
            back.Visible = true;
            this.Visible = false;
        }
    }
}
